/**
 * @CLASSDESCRIPER:队列：先进先出;使用单链表的形式编写队列的入队出队
 * @AUTHOR:tangyi
 * @PHONE:17773063844
 * @EMIAL:ty20020127@163.COM
 * @DATE:2021/1/23
 * @PARAMDESCRIPER:
 * @OUTPARAMDESCRIPER:
 */
public class TANG_YI_23_Q2<T> {
    //头节点
    private Node<T> head;
    //尾节点
    private Node<T> end;
    //队列中所存在的元素个数
    private int length;

    //自定义构造器
    public TANG_YI_23_Q2(){
        //初始化头节点
        this.head=new Node(null,null);
        //尾节点指向头节点
        end=head;
        //初始化所在元素为0
        this.length=0;
    }

    //获取栈元素的个数
    public int getLength(){
        return length;
    }

    //判断栈是否为空
    public boolean isEmpty(){
        return this.length==0;
    }

    //入队
    public void push(T t){
        //新对象的构建
        Node node = new Node(t,null);
        //尾节点指向新构建的节点
        end.next=node;
        //让新节点成为尾节点
        end=node;
        //元素个数加1
        this.length++;
    }

    //出队
    public T poll(){
        //判断栈是否为空，为空则无法出栈
        if (isEmpty()){
            //报异常
            return (T)"队列为空，无法出队";
        }
        //新的节点指向头节点的下一个节点
        Node curr = head.next;
        //head节点指向原来的第二个节点
        head.next=curr.next;
        //元素个数减1
        this.length--;
        //返回出栈的t值
        return (T) curr.t;
    }


    class Node<T>{
        T t;
        Node<T> next;

        public Node(T t, Node<T> next) {
            this.t = t;
            this.next = next;
        }
    }

    public static void main(String[] args) {
        TANG_YI_23_Q2 tang_yi_23_q2 = new TANG_YI_23_Q2();
        tang_yi_23_q2.push(10);
        tang_yi_23_q2.push(20);
        tang_yi_23_q2.push(30);
        tang_yi_23_q2.push(40);
        System.out.println("队列长度："+tang_yi_23_q2.getLength());
        System.out.println("头节点下一个节点的t值为："+tang_yi_23_q2.head.next.t);
        System.out.println("出栈的节点t值："+tang_yi_23_q2.poll());
        System.out.println("队列长度："+tang_yi_23_q2.getLength());
        System.out.println("头节点下一个节点的t值为："+tang_yi_23_q2.head.next.t);
        System.out.println("出栈的节点t值："+tang_yi_23_q2.poll());
        System.out.println("队列长度："+tang_yi_23_q2.getLength());
        System.out.println("头节点下一个节点的t值为："+tang_yi_23_q2.head.next.t);
        System.out.println("出栈的节点t值："+tang_yi_23_q2.poll());
        System.out.println("队列长度："+tang_yi_23_q2.getLength());
        System.out.println("头节点下一个节点的t值为："+tang_yi_23_q2.head.next.t);
        System.out.println("出栈的节点t值："+tang_yi_23_q2.poll());
        System.out.println("队列长度："+tang_yi_23_q2.getLength());
        System.out.println("出栈的节点t值："+tang_yi_23_q2.poll());
    }
    /**
     * 问题：
     *      应该没有
     */
}
